#!/bin/bash


LIB_DIR=lib
JPEG_DIR=lib/jpeg
FFT_DIR=lib/FFT
GSM_DIR=lib/gsm
MIPS_DIR=${HOME}/development/mc723/mips

today=$(date "+%Y.%m.%d,%H:%M:%S")

execute(){
    temp=$(mktemp)
    temp2=$(mktemp)
    ${MIPS_DIR}/mips1.x --load=${2} > ${3}  2>> ${temp}
    cat ${temp} | tr -d '\n\t\r' | sed -e 's/Printing/\n/g' -e 's/instruction/:/g' >> ${temp2}
    temp=$(mktemp)
    while read line
    do
	contains=$(echo $line | grep "statistics from :")
	if [ "${contains}" != "" ]
	then
            cmd=$(echo ${line} | cut -d ':' -f2 | tr -d ' ')
            qtd=$(echo ${line} | cut -d ':' -f4 | cut -d '[' -f 1 | tr -d ' ')
            echo "${cmd}|${qtd}" >> ${temp}
	fi
    done < ${temp2}
    mv ${temp} ${CURR_DIR}/data/$1-${today}.data
    #bash count.sh ${CURR_DIR}/data/$1-${today}.data
}


nomips=$(echo ${PATH} | grep "mips")
if [ "${nomips}" == "" ]
then
    export PATH= ${PATH}:${MIPS_DIR}
fi


CURR_DIR=$(pwd)    


cd ${CURR_DIR}
echo "Executing for GSM coder (large)"
cd ${GSM_DIR}
execute "gsm" "bin/toast -fps -c input/large.au" "output/output_large.encode.gsm"
diff --report-identical-file data/output_large.encode.gsm output/output_large.encode.gsm > ${CURR_DIR}/gsm.data



cd ${CURR_DIR}
echo "Executing for JPEG (small)"
cd ${JPEG_DIR}
execute "jpeg" "bin/cjpeg -dct int -progressive -opt -outfile output/output_small_encode.jpeg input/input_small.ppm" "/dev/null"
diff --report-identical-file data/output_small_encode.jpeg output/output_small_encode.jpeg > ${CURR_DIR}/jpeg.data


cd ${CURR_DIR}
echo "Executing FFT (small)"
cd ${FFT_DIR}
pwd
execute "fft" "bin/fft 4 4096" "output/output_small.txt"
diff --report-identical-file data/output_small.txt output/output_small.txt > ${CURR_DIR}/fft.data

rm *.data